package com.itheima.server.controller;

import com.itheima.model.vo.PageResult;
import com.itheima.server.service.CommentsService;
import com.itheima.server.service.SmallVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideoController {
    @Autowired
    private SmallVideoService smallVideoService;
    @Autowired
    private CommentsService commentsService;


    /*
     * 视频上传
     * */
    @PostMapping
    public ResponseEntity upload(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        smallVideoService.upload(videoThumbnail, videoFile);
        return ResponseEntity.ok(null);
    }

    /*小视频列表*/
    @GetMapping
    public ResponseEntity pageList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult res = smallVideoService.pageList(page, pagesize);
        return ResponseEntity.ok(res);
    }

    /*关注*/
    @PostMapping("/{id}/userFocus")
    public ResponseEntity userFocus(@PathVariable("id") Long followUserId) {
        smallVideoService.userFocus(followUserId);
        return ResponseEntity.ok(null);
    }

    /*取消关注*/
    @PostMapping("/{id}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("id") Long followUserId) {
        smallVideoService.userUnFocus(followUserId);
        return ResponseEntity.ok(null);
    }

    /*点赞*/
    @PostMapping("{id}/like")
    public ResponseEntity like(@PathVariable("id") String id) {
        smallVideoService.like(id);
        return ResponseEntity.ok(null);
    }

    /*取消点赞*/
    @PostMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String id) {
        smallVideoService.dislike(id);
        return ResponseEntity.ok(null);
    }

    /*视频评论*/
    @PostMapping("/{id}/comments")
    public ResponseEntity comments( @RequestBody Map<String,String> map) {
        smallVideoService.saveComments(map.get("id"), map.get("comment"));
        return ResponseEntity.ok(null);

    }

    /*视频评论列表*/
    @GetMapping("/{id}/comments")
    public ResponseEntity recommend(@PathVariable String id, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = commentsService.findComments(id, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 评论点赞
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity commentsLike(@PathVariable("id") String commentId) {
        //1.调用service处理
        Integer likeCount = commentsService.likeCount(commentId);
        return ResponseEntity.ok(likeCount);
    }

    /*    *
     * 取消评论点赞*/
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity commentsDislike(@PathVariable("id") String commentId) {
        //1.调用service处理
        Integer likeCount = commentsService.dislikeCount(commentId);
        return ResponseEntity.ok(likeCount);
    }
}
